import socket
import threading


def t_send_main(client_socket):
    # # 输入昵称
    # send_user = input("请输入发送对象：")
    # user_bytea = send_user.encode("gbk")
    # client_socket.send(user_bytea)

    # 输入发送信息
    while True:
        # send_msg = input(f"请输入发送给{send_user}的信息：格式： 昵称|信息内容：")
        send_msg = input(f"请输入发送信息：")
        send_bytes = send_msg.encode("gbk")
        client_socket.send(send_bytes)


def t_recv_main(client_socket):
    while True:
        recv_bytes = client_socket.recv(1024)
        recv_msg = recv_bytes.decode("gbk")
        # r = recv_msg.split("|")
        # print(f"{r[0]}向你发送信息{r[1]}")
        print(f"接收信息{recv_msg}")


def main():
    # 构建客户端套接字实例
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 连接
    SERVER_ADDR = ("192.168.11.151", 60000)
    client_socket.connect(SERVER_ADDR)

    # 输入昵称
    # 输入发送信息
    t_send = threading.Thread(target=t_send_main, args=(client_socket, ))
    t_send.start()

    # 接收信息
    t_recv= threading.Thread(target=t_recv_main, args=(client_socket, ))
    t_recv.start()



if __name__ == '__main__':
    main()

